package com.ee.action;


import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.eclipse.jdt.internal.compiler.ast.ThisReference;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ee.bean.User;
import com.ee.service.UserService;
import com.ee.util.Md5Util;
import com.opensymphony.xwork2.ActionSupport;

@Component("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
	private User user;
	private UserService userService;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private String securityCode;
	private String newPWD;
	public String getNewPWD() {
		return newPWD;
	}

	public void setNewPWD(String newPWD) {
		this.newPWD = newPWD;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public UserService getUserService() {
		return userService;
	}


	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	/**
	 * 更新密码
	 * @throws IOException
	 */
	public void updatePWD() throws IOException{
		PrintWriter writer = response.getWriter();
		User currentUser = (User)request.getSession().getAttribute("user");
		if(currentUser.getPassword().equals(Md5Util.md5(this.user.getPassword()))){
			currentUser.setPassword(Md5Util.md5(this.getNewPWD()));
			request.getSession().setAttribute("user", currentUser);
			userService.update(currentUser);
			writer.write("success");
		}else{
			writer.write("fail");
		}
	}
	/**
	 * 删除用户
	 * @return
	 */
	public String delete(){
		userService.delete(user);
		return "_success";
	}
	/**
	 * 操作人员列表
	 * @return
	 */
	public String operatorList(){
		request.setAttribute("operators", userService.findOperators());
		return "list_success";
	}
	/**
	 * 添加操作人员
	 * @return
	 */
	public String add(){
		if("".equals(this.user.getId()) || this.user.getId() == 0){
			this.user.setAuth("operator");
			this.user.setPassword(Md5Util.md5("000000"));
			userService.add(this.user);
		}else{
			userService.update(this.user);
			User current = (User)request.getSession().getAttribute("user");
			if(current.getId() == this.user.getId()){
				request.getSession().setAttribute("user", this.user);
				return current.getAuth() + "_center";
			}
		}
		
		return "_success";
	}
	/**
	 * 登陆时检查密码
	 * @throws IOException
	 */
	public void checkAccount() throws IOException{
		PrintWriter writer = response.getWriter();
		if(!userService.checkAccount(user.getAccount())){
			writer.write("valid");
		}else{
			writer.write("invalid");
		}
		writer.flush();
		writer.close();
	}
	/**
	 * 登陆行为
	 * @return
	 */
	public String login(){
		HttpSession session = request.getSession();
		if("".equals(this.getSecurityCode()) || this.getSecurityCode() == null){
			return "login_fail";
		}
		if(!session.getAttribute("securityCode").equals(this.getSecurityCode())){
			request.setAttribute("message", "验证码错误，请重新输入");
			return "login_fail";
		}
		if(userService.findUsers().size()==0){
			User tempUser = new User();
			tempUser.setAccount("admin");
			tempUser.setAuth("admin");
			tempUser.setName("admin");
			tempUser.setPassword(Md5Util.md5("admin"));
			userService.add(tempUser);
		}
		if(!userService.checkAccount(this.user.getAccount())){
			request.setAttribute("message", "不存在该用户");
			return "login_fail";
		}
		User user = userService.checkLogin(this.user.getAccount(), this.user.getPassword());
		if(user != null){
			session.setAttribute("user", user);
			return user.getAuth() + "_login_success";
		}else{
			request.setAttribute("message", "用户名或密码错误");
			return "login_fail";
		}
	}
	/**
	 * 登出操作
	 * @return
	 */
	public String logout(){
		request.setAttribute("user", null);
		return "login_fail";
	}
	/**
	 * 个人中心
	 * @return
	 */
	public String center(){
		String auth = ((User)this.request.getSession().getAttribute("user")).getAuth();
		return auth + "_center";
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getSecurityCode() {
		return securityCode;
	}

	public void setSecurityCode(String securityCode) {
		this.securityCode = securityCode;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	

}
